/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Programming
 */
class daBlob{
    public Blob blobInner;
    public Blob blobOuter;
    public daBlob(Blob _b1, Blob _b2){
        double b1Area = _b1.getArea();
        double b2Area = _b1.getArea();
        if (b1Area > b2Area){
            blobOuter = _b1;
            blobInner = _b2;
        }else{
            blobOuter = _b2;
            blobInner = _b1;
            
        }

    }
}

public class Blob {
    double x1, y1, x2, y2, x3, y3, x4, y4;
    double[] center = new double[2];
    double ratio, area, cmx, cmy;
    public Blob(double _x1, double _y1, double _x2, double _y2, double _x3, double _y3, double _x4, double _y4) {
        x1 = _x1;
        y1 = _y1;
        x2 = _x2;
        y2 = _y2;
        x3 = _x3;
        y3 = _y3;
        x4 = _x4;
        y4 = _y4;
    }

    public double[] getCenter(){
        center[0] = ((((x1+x4)/2) + ((x2+x3)/2)) / 2);
        center[1] = ((((y1+y4)/2) + ((x2+x3)/2)) / 2);
        return center;
    }
    
    public double getArea(){
        double h1, h2, v1, v2, l1, l2;
        h1 = Math.sqrt(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2));
        h2 = Math.sqrt(Math.pow((x3 - x4), 2) + Math.pow((y3 - y4), 2));

        l1 = (h1 + h2) / 2;

        v1 = Math.sqrt(Math.pow((x2 - x3), 2) + Math.pow((y2 - y3), 2));
        v2 = Math.sqrt(Math.pow((x1 - x4), 2) + Math.pow((y1 - y4), 2));

        l2 = (v1 + v2) / 2;

        return area = (l1 * l2);
    }
    
    public double getAspectRatio(){
        double h1, h2, v1, v2, l1, l2;
        h1 = Math.sqrt(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2));
        h2 = Math.sqrt(Math.pow((x3 - x4), 2) + Math.pow((y3 - y4), 2));

        l1 = (h1 + h2) / 2;

        v1 = Math.sqrt(Math.pow((x2 - x3), 2) + Math.pow((y2 - y3), 2));
        v2 = Math.sqrt(Math.pow((x1 - x4), 2) + Math.pow((y1 - y4), 2));

        l2 = (v1 + v2) / 2;

      //  System.out.print("l1 " + l1 + " l2 " + l2);
        return ratio = (l1 / l2);
    }

    public void findCM(){
        centerOfMassFinder comf = new centerOfMassFinder(x1,y1,x2,y2,x3,y3,x4,y4);
        comf.findCenterOfMass();
        cmx = comf.centerX;
        cmy = comf.centerY;
    }


    
    

    double getVertAngle(double pixelX, double pixelY) {
	double angle = Math.atan((1 - 2.0 * pixelY / Target.imageHeight) * Math.tan(Target.verticalFOV / 2));
	angle += Target.cameraVerticalAngle;
	return angle;
    }

    double getDist(double _height, double _angle) {
	return (_height - Target.cameraHeight)/ Math.tan(_angle);
    }

    double getHorizAngle(double pixelX, double pixelY) {
	double angle = Math.atan((2.0 * pixelX / Target.imageWidth - 1) * Math.tan( Target.horizontalFOV / 2));
	angle += Target.cameraHorizontalAngle;
	return angle;
    }

    double getAzimuthFromWidth(double angleWidth, double targetDistance){
	double theta = angleWidth;
	double phi; // real value
	double phi1;
	double phi2;
	double G = Target.targetWidth / targetDistance;
	double H = G * G;
	double U = Math.tan(theta / 2);
	double A = H * U * U * U * U;
	double B = -2 * (U * U) * (H + 2);
	double C = H - (4 * U * U);

	double result1 = SolveSecondDegree(A,B,C,true);
	double result2 = SolveSecondDegree(A,B,C,false);

	phi1 = Math.atan(Math.sqrt(result1));
	phi2 = Math.atan(Math.sqrt(result2));

	//result one
	double test1 =  Math.cos(phi1) * (Math.tan(phi1 + theta/2) - Math.tan(phi1 - theta / 2));
	double test2 =  Math.cos(phi2) * (Math.tan(phi2 + theta/2) - Math.tan(phi2 - theta / 2));

	if (Math.abs(test1 - G) > Math.abs(test2 - G)){
		phi = phi2;
	}
	else {
		phi = phi1;
	}
	return phi;
    }


    static double SolveSecondDegree(double a, double b, double c, boolean plus){
        double x;
	if (plus) x = (-b + Math.sqrt(b*b - (4*a*c)))/(2*a);
	else x = (-b - Math.sqrt(b*b - (4*a*c)))/(2*a);
	return x;
    }

    static double getTargetElevation(Target.Type _type){
        if (_type == Target.Type.LEFT || _type == Target.Type.RIGHT)
            return Target.targetLORElevation;
        return Target.targetMiddleElevation;
    }

}
